<?php defined( 'BASEPATH' ) or die( 'Restricted' );

/*
This code is copyright 2009-2011 by TMLA INC.  ALL RIGHTS RESERVED.
Please view license.txt in /tgsf_core/legal/license.txt or
http://tgWebSolutions.com/opensource/tgsf/license.txt
for complete licensing information.
*/


class tagModel extends tgsfBase
{
	/**
	* Inserts a tag and returns the id
	*/
	public function insert( $ds )
	{
		if ( $this->exists( $ds->tag_text ) )
		{
			return $this->fetch_id( $ds->tag_text );
		}

		return query::factory()
			->insert_into( 'tag' )
			->pt( ptSTR )
			->insert_fields( 'tag_text' )
			->autoBind( $ds )
			->exec()
			->lastInsertId;
	}
	//------------------------------------------------------------------------
	/**
	*
	*/
	public function exists( $tag_text )
	{
		return query::factory()
			->count()
			->from( 'tag' )
			->where( 'tag_text=:tag_text' )
			->bindValue( 'tag_text', $tag_text, ptSTR )
			->exec()
			->fetchColumn(0) > 0;
	}
	//------------------------------------------------------------------------
	/**
	* returns the tag id for the given text
	* @param String The tag text
	*/
	public function fetch_id( $tag_text )
	{
		return query::factory()
			->select( 'tag_id' )
			->from( 'tag' )
			->where( 'tag_text=:tag_text' )
			->bindValue( 'tag_text', $tag_text, ptSTR )
			->exec()
			->fetchColumn(0);
	}
}

return new tagModel();
